﻿
Imports std
Imports storoomc

Partial Public Class storoomDistrTM
    Private MustInherit Shadows Class distrInfo(Of argType, returnType)
        Inherits distributeOperation.distrInfo(Of pair(Of argType, vector(Of storageInterface)), returnType)
        Public slice As storageInterface = Nothing 'the slice this info work on

        Protected NotOverridable Overrides Function initialInfo(ByVal index As Int32, _
                                                                ByVal arg As pair(Of argType,  _
                                                                                  vector(Of storageInterface))) _
                                                               As Boolean
            If MyBase.initialInfo(index, arg) AndAlso Not arg Is Nothing AndAlso Not arg.second Is Nothing Then
                slice = arg.second(index)
                Return initialInfo(index, arg.first)
            Else
                Return False
            End If
        End Function

        Protected Overridable Overloads Function initialInfo(ByVal index As Int32, ByVal arg As argType) As Boolean
            Return True
        End Function

        Public NotOverridable Overrides Function canwork() As Boolean
            Return MyBase.canwork() AndAlso Not slice Is Nothing _
                   AndAlso Not caller Is Nothing AndAlso _
                   convertor.cast(Of storoomDistrTM)(caller).ishealthSlice(slice)
        End Function

        Protected Shadows Function caller() As storoomDistrTM
            Return convertor.cast(Of storoomDistrTM)(MyBase.caller)
        End Function
    End Class

    Private Class singleDistrInfo(Of returnType)
        Inherits distributeOperation.distrInfo(Of Boolean, returnType)

        Protected NotOverridable Overrides Function initialInfo(ByVal index As Int32, ByVal arg As Boolean) As Boolean
            Return MyBase.initialInfo(index, arg)
        End Function

        Public NotOverridable Overrides Function canwork() As Boolean
            Return MyBase.canwork()
        End Function

        Private work As _do(Of ternary) = Nothing
        Private ternary_to_result As _do(Of ternary, returnType) = Nothing

        Protected NotOverridable Overrides Function operationImpl() As ternary
            assert(Not work Is Nothing)
            Return work()
        End Function

        Public NotOverridable Overrides Function finishOperation(ByRef rtn As returnType) As Boolean
            assert(Not ternary_to_result Is Nothing)
            rtn = ternary_to_result(result)
            Return True
        End Function

        Public Shared Function createInfo(ByVal work As _do(Of ternary), _
                                          ByVal ternary_to_result As _do(Of ternary, returnType)) _
                                            As singleDistrInfo(Of returnType)
            Dim rtn As singleDistrInfo(Of returnType) = Nothing
            rtn = New singleDistrInfo(Of returnType)()
            rtn.work = work
            rtn.ternary_to_result = ternary_to_result
            Return rtn
        End Function
    End Class

    Private Class retireInfo
        Inherits distrInfo(Of Boolean, Boolean)

        Public Overrides Function finishOperation(ByRef rtn As Boolean) As Boolean
            If result = ternary.false Then
                rtn = False
            End If

            Return True
        End Function

        Protected Overrides Function operationImpl() As ternary
            If slice.retire() Then
                caller().runcallback(jobType.retire, slice, Nothing)
                Return ternary.true
            Else
                Return ternary.false
            End If
        End Function

        Public Shared Function createInfo() As retireInfo
            Return New retireInfo()
        End Function
    End Class

    Private Class emptyInfo
        Inherits distrInfo(Of Boolean, Boolean)

        Public Overrides Function finishOperation(ByRef rtn As Boolean) As Boolean
            If result = ternary.false Then
                rtn = False
            End If

            Return True
        End Function

        Protected Overrides Function operationImpl() As ternary
            If slice.empty() Then
                caller().runcallback(jobType.empty, slice, Nothing)
                Return ternary.true
            Else
                Return ternary.false
            End If
        End Function

        Public Shared Function createInfo() As emptyInfo
            Return New emptyInfo
        End Function
    End Class

    Private Class closeInfo
        Inherits distrInfo(Of Boolean, Boolean)

        Protected Overrides Function operationImpl() As ternary
            If slice.close() Then
                caller().runcallback(jobType.close, slice, Nothing)
                Return ternary.true
            Else
                Return ternary.false
            End If
        End Function

        Public Shared Function createInfo() As closeInfo
            Return New closeInfo
        End Function
    End Class

    Private Class openInfo(Of argType)
        Inherits distrInfo(Of argType, Boolean)

        Protected Overrides Function operationImpl() As ternary
            If slice.open() Then
                caller().runcallback(jobType.open, slice, Nothing)
                Return ternary.true
            Else
                raiseError("cannot open slice.", errorHandle.errorType.exclamation)
                Return ternary.false
            End If
        End Function

        Public Overrides Function finishOperation(ByRef rtn As Boolean) As Boolean
            If result <> ternary.true Then
                rtn = False
            End If
            Return True
        End Function

        Public Shared Function createInfo() As openInfo(Of argType)
            Return New openInfo(Of argType)()
        End Function
    End Class

    Private Class seekInfo
        Inherits distrInfo(Of String, Boolean)

        Protected key As String = Nothing

        Public Overrides Function finishOperation(ByRef rtn As Boolean) As Boolean
            If result = ternary.true Then
                rtn = True
            End If
            Return True
        End Function

        Protected Overrides Function initialInfo(ByVal index As Int32, ByVal arg As String) As Boolean
            If MyBase.initialInfo(index, arg) Then
                copy(key, arg)
                Return True
            Else
                Return False
            End If
        End Function

        Protected Overrides Function operationImpl() As ternary
            If slice.seek(key) Then
                caller().runcallback(jobType.seek, slice, make_pair(key, True))
                Return ternary.true
            Else
                Return ternary.false
            End If
        End Function

        Public Shared Function createInfo() As seekInfo
            Return New seekInfo()
        End Function
    End Class

    Private Class readInfo
        Inherits distrInfo(Of String, bytes)
        Protected key As String = Nothing
        Protected buff As bytes = Nothing

        Public Overrides Function finishOperation(ByRef rtn As bytes) As Boolean
            If result = ternary.true Then
                rtn += buff
            End If
            Return True
        End Function

        Protected Overrides Function initialInfo(ByVal index As Int32, ByVal arg As String) As Boolean
            If MyBase.initialInfo(index, arg) Then
                copy(key, arg)
                Return True
            Else
                Return False
            End If
        End Function

        Protected Overrides Function operationImpl() As ternary
            Return ifoper(slice.read(key, buff), ternary.true, ternary.false)
        End Function

        Public Shared Function createInfo() As readInfo
            Return New readInfo()
        End Function
    End Class

    Private Class listInfo
        Inherits distrInfo(Of Boolean, vector(Of String))
        Protected list As vector(Of String) = Nothing

        Public Overrides Function finishOperation(ByRef rtn As vector(Of String)) As Boolean
            If result = ternary.true AndAlso list.size() > 0 Then
                assert(rtn.push_back(list), "rtn.push_back return false.")
            End If

            Return True
        End Function

        Protected Overrides Function operationImpl() As ternary
            Return ifoper(slice.list(list), ternary.true, ternary.false)
        End Function

        Public Shared Function createInfo() As listInfo
            Return New listInfo()
        End Function
    End Class

    'nodes_redundance3 overrides operationImpl with tsOper
    Private Class deleteInfo
        Inherits distrInfo(Of String, Boolean)
        Public key As String = Nothing

        Public Overrides Function finishOperation(ByRef rtn As Boolean) As Boolean
            If result = ternary.true Then
                rtn = True
            End If
            Return True
        End Function

        Protected Overrides Function initialInfo(ByVal index As Int32, ByVal arg As String) As Boolean
            If MyBase.initialInfo(index, arg) Then
                copy(key, arg)
                Return True
            Else
                Return False
            End If
        End Function

        Protected Overrides Function operationImpl() As ternary
            Return ifoper(slice.delete(key), ternary.true, ternary.false)
        End Function

        Public Shared Function createInfo() As deleteInfo
            Return New deleteInfo()
        End Function
    End Class

    Private Class fullInfo
        Inherits distrInfo(Of Boolean, Boolean)

        Public Overrides Function finishOperation(ByRef rtn As Boolean) As Boolean
            If result = ternary.false Then
                rtn = False
            End If

            Return True
        End Function

        Protected Overrides Function operationImpl() As ternary
            Return ifoper(slice.full(), ternary.true, ternary.false)
        End Function

        Public Shared Function createInfo() As fullInfo
            Return New fullInfo()
        End Function
    End Class

    Private Class shareTypeFullInfo
        Inherits fullInfo

        Public Overrides Function finishOperation(ByRef rtn As Boolean) As Boolean
            If result = ternary.true Then
                rtn = True
            End If

            Return True
        End Function

        Public Shared Shadows Function createInfo() As shareTypeFullInfo
            Return New shareTypeFullInfo()
        End Function
    End Class

    Private Class capInfo
        Inherits distrInfo(Of Boolean, UInt64)

        Protected slicecap As UInt64

        Public Overrides Function finishOperation(ByRef rtn As UInt64) As Boolean
            If result = ternary.true Then
                rtn += slicecap
            End If

            Return True
        End Function

        Protected Overrides Function operationImpl() As ternary
            Return ifoper(slice.capacity(slicecap), ternary.true, ternary.false)
        End Function

        Public Shared Function createInfo() As capInfo
            Return New capInfo()
        End Function
    End Class

    Private Class shareTypeCapInfo
        Inherits capInfo

        Public Overrides Function finishOperation(ByRef rtn As UInt64) As Boolean
            If result = ternary.true Then
                If rtn > slicecap Then
                    rtn = slicecap
                End If
            Else
                rtn = 0
            End If

            Return True
        End Function

        Public Shared Shadows Function createInfo() As capInfo
            Return New shareTypeCapInfo()
        End Function
    End Class

    Private Class sizeInfo
        Inherits distrInfo(Of Boolean, UInt64)

        Protected slicesize As UInt64

        Public Overrides Function finishOperation(ByRef rtn As UInt64) As Boolean
            If result = ternary.true Then
                rtn += slicesize
                Return True
            Else
                Return False
            End If
        End Function

        Protected Overrides Function operationImpl() As ternary
            Return ifoper(slice.valuesize(slicesize), ternary.true, ternary.false)
        End Function

        Public Shared Function createInfo() As sizeInfo
            Return New sizeInfo()
        End Function
    End Class

    'nodes_redundance2 uses sizeofInfo to decide whether a key should be synced
    Private Class sizeofInfo
        Inherits distrInfo(Of String, pair(Of UInt64, Boolean))

        Protected key As String
        Protected size As UInt64

        Public Overrides Function finishOperation(ByRef rtn As pair(Of UInt64, Boolean)) As Boolean
            If result = ternary.true Then
                rtn.second = True
                rtn.first += size
            End If

            Return True
        End Function

        Protected Overrides Function initialInfo(ByVal index As Int32, ByVal arg As String) As Boolean
            If MyBase.initialInfo(index, arg) Then
                copy(key, arg)
                Return True
            Else
                Return False
            End If
        End Function

        Protected Overrides Function operationImpl() As ternary
            Return ifoper(slice.sizeof(key, size), ternary.true, ternary.false)
        End Function

        Public Shared Function createInfo() As sizeofInfo
            Return New sizeofInfo()
        End Function
    End Class

    Private Class keycountInfo
        Inherits distrInfo(Of Boolean, UInt64)

        Protected keycount As UInt64

        Public Overrides Function finishOperation(ByRef rtn As UInt64) As Boolean
            If result = ternary.true Then
                rtn += keycount
            End If

            Return True
        End Function

        Protected Overrides Function operationImpl() As ternary
            Return ifoper(slice.keycount(keycount), ternary.true, ternary.false)
        End Function

        Public Shared Function createInfo() As keycountInfo
            Return New keycountInfo()
        End Function
    End Class
End Class
